package com.kingre.et.action;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.mail.MessagingException;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.context.SecurityContextHolder;

import com.kingre.et.model.School;
import com.kingre.et.model.User;
import com.kingre.et.service.ISchoolService;
import com.kingre.et.service.IUserService;
import com.opensymphony.xwork2.ActionSupport;

public class UserManagerAction extends ActionSupport {
    private static final long serialVersionUID = -5778777897090373421L;
    private static final Logger logger = Logger.getLogger(RegisterAction.class);
    private String roleName;
    @Autowired
    private IUserService userService;
    @Autowired
    private ISchoolService schoolService;
    private User user;
    private List<User> users;
    private List<School> schoolList = new ArrayList<School>();

    private Integer schoolId;
    private String name;

    List<Map<String, String>> rawdataList;

    @Override
    public String execute() throws IOException, MessagingException {
        roleName = SecurityContextHolder.getContext().getAuthentication().getAuthorities()[0].getAuthority();
        schoolList = findSchool();
        logger.debug("schoolId=" + schoolId);
        logger.debug("name=" + name);
        if (schoolId != null && name != null)
            users = userService.findUserBySchoolAndName(schoolId, name);
        return SUCCESS;
    }

    private List<School> findSchool() {
        User currentUser = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        List<School> list = schoolService.find(currentUser.getId(), "");
        if (list == null)
            return list;
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getSchoolType() == 20)
                list.get(i).setName("——" + list.get(i).getName());
        }
        return list;

    }

    public String export() {
        rawdataList = userService.findRawdataAll();
        return SUCCESS;
    }

    public String generate() {
        roleName = SecurityContextHolder.getContext().getAuthentication().getAuthorities()[0].getAuthority();
        // rawdataList = userService.findRawdataAll();
        addFieldError("dd", getText("user.generate.ok"));
        return SUCCESS;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

    public List<School> getSchoolList() {
        return schoolList;
    }

    public void setSchoolList(List<School> schoolList) {
        this.schoolList = schoolList;
    }

    public Integer getSchoolId() {
        return schoolId;
    }

    public void setSchoolId(Integer schoolId) {
        this.schoolId = schoolId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getRoleName() {
        return roleName;
    }

    public void setRoleName(String roleName) {
        this.roleName = roleName;
    }

    public List<Map<String, String>> getRawdataList() {
        return rawdataList;
    }

    public void setRawdataList(List<Map<String, String>> rawdataList) {
        this.rawdataList = rawdataList;
    }

}
